﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace S7.Windows.Menu
{
    /// <summary>
    /// Interaction logic for AddCategory.xaml
    /// </summary>
    public partial class AddCategory : Window
    {
        public AddCategory()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (tbName.Text != "")
            {
                string Name = Info.Capitalize(tbName.Text);
                Thread thr = new Thread(new ThreadStart(() =>
                {
                    try
                    {
                        Menus menu = new Menus();
                        menu.MenuName = Name;
                        string result = menu.Add();
                        if (result == "")
                        {
                            MessageBox.Show("Menu is added", "Done!", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                            Application.Current.Dispatcher.Invoke(new ThreadStart(() =>
                            {
                                this.Close();
                            }));
                        }
                        else
                        {
                            MessageBox.Show(result, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    }
                }));
                thr.IsBackground = true;
                thr.Start();
            }
            else
            {
                MessageBox.Show("Please enter a name.", "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
